<template>
	<view>
		<view class="inputs flex">
			<image src="/static/search_b.png" mode=""></image>
			<input
				v-model="data.queryParams.keyword"
				placeholder-class="input_class"
				auto-focus="true"
				type="text"
				placeholder="输入帖子内容"
				confirm-type="search"
				@confirm="handleSearch"
			/>
			<view v-if="data.queryParams.keyword" class="flex_jc_cent" style="width: 50rpx" @click="clearInput">
				<uni-icons type="close" size="22"></uni-icons>
			</view>
		</view>
		<view class="kong" v-if="data.queryParams.keyword && data.list.length == 0 && data.isshow">
			<image src="/static/k.png" mode=""></image>
			<view class="text">
				没有找到你搜索的内容
				<view class="">换个关键词试试~</view>
			</view>
		</view>
		<loadstatusVue :status="data.loadStatus" v-if="data.list.length != 0">
			<view class="content">
				<view class="take_item" @click="goInfo(item)" v-for="(item, index) in data.list" :key="index">
					<view class="take_user flex_jc_sb">
						<view class="u_l flex">
							<image v-if="item.createUser.img" :src="item.createUser.img" mode="aspectFill"></image>
							<image v-else src="https://www.daolangtongge.com/read-book-api/profile/mini_app/img/head.png" mode=""></image>
							<view class="flex_r">
								<view class="t1">{{ item.createUser.name }}</view>
								<view class="t2">{{ item.createTime }}</view>
							</view>
						</view>
						<view class="u_r flex_jc_cent" v-if="item.top == 1">
							<view class="flex">
								<image src="/static/tt.png" mode=""></image>
								<view class="">已置顶</view>
							</view>
						</view>
					</view>
					<view class="texts">
						<view class="title">{{ item.title }}</view>
						<view class="t_content">
							{{ item.content }}
						</view>
					</view>
					<view class="imglist" v-if="item.images.length != 0">
						<view class="img_item" v-for="(img, ind) in item.images.slice(0, 4)" :key="ind">
							<image :src="img" mode="aspectFill"></image>
							<view class="item_ads flex_jc_cent" v-if="ind == 3 && item.images.length > 4">{{ item.images.length - 4 }} +</view>
						</view>
					</view>
					<view class="" v-if="item.lastReply">
						<view class="comment">
							<view class="ts">
								<text>{{ item.lastReply.user.name }}：</text>
								{{ item.lastReply.content }}
							</view>
							<view class="c_imglist" v-if="item.lastReply.images">
								<view class="img_item" v-for="(img, ind) in item.lastReply.images.split(',').slice(0, 4)" :key="ind">
									<image :src="img" mode="aspectFill"></image>
								</view>
							</view>
						</view>
					</view>
					<view class="hd" v-if="item.activity" @click.stop="$pageGo(`/pages/activity/info?id=${item.activityId}`)">
						<image src="/static/q.png" mode=""></image>
						{{ item.activity.name }}
					</view>
					<view class="handle_block flex_jc_sb" @click.stop="">
						<view class="handle_l flex">
							<image src="/static/cc3.png" mode=""></image>
							<view class="input" @click="goInfo(item, 'input')">与人善言，暖于布帛</view>
						</view>
						<view class="handle_r flex">
							<image v-if="item.selfLike" src="/static/zan_1.png" mode="" @click="changeIMG(item, 1)"></image>
							<image v-else src="/static/zan.png" mode="" @click="changeIMG(item, 1)"></image>
							<image v-if="item.selfFlag" src="/static/xin_1.png" mode="" @click="changeIMG(item, 2)"></image>
							<image v-else src="/static/xin.png" mode="" @click="changeIMG(item, 2)"></image>
							<view class="">
								<image src="/static/zhuan.png" mode=""></image>
								<button class="zf_btns" open-type="share" :data-obj="item"></button>
							</view>
						</view>
					</view>
				</view>
			</view>
		</loadstatusVue>
	</view>
</template>

<script setup>
import { ref, reactive, getCurrentInstance } from 'vue';
import { onLoad, onShow, onReachBottom, onPageScroll, onShareAppMessage, onShareTimeline } from '@dcloudio/uni-app';
import { communityList, communityData, communityChangeLike } from '@/api/community.js';
import loadstatusVue from '@/components/loadstatus/loadstatus.vue';
import { toast, showConfirm } from '@/utils/common.js';
const data = reactive({
	isshow: false,
	list: [],
	loadStatus: 'before', // 加载状态
	hasMore: true, // 是否有更多数据
	isLoading: false, // 防止重复请求的锁
	queryParams: {
		keyword: '',
		pageNum: 1,
		pageSize: 10
	}
});
// 上拉加载更多
onReachBottom(() => {
	if (data.hasMore && (data.loadStatus === 'success' || data.loadStatus === 'noMore') && !data.isLoading) {
		data.queryParams.pageNum++;
		getList(true);
	}
});
// 延迟函数
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
// 设置加载状态的方法
const setLoadStatus = (status, options = {}) => {
	data.loadStatus = status;
};
// 处理响应数据的通用方法
const handleResponseData = async (res, isLoadMore = false) => {
	// 如果是加载更多，确保loading状态至少显示500ms，避免闪烁
	if (isLoadMore) {
		await delay(500);
	}
	if (res.code == 200) {
		// 处理数据
		if (isLoadMore) {
			data.list.push(...res.rows);
		} else {
			data.list = res.rows || [];
		}
		// 判断是否有更多数据
		data.hasMore = res.rows && res.rows.length >= data.queryParams.pageSize;
		// 设置状态
		if (data.list.length === 0) {
			data.isshow = true;
			setLoadStatus('empty');
		} else if (!data.hasMore) {
			setLoadStatus('noMore');
		} else {
			setLoadStatus('success');
		}
		return true;
	}
	return false;
};

// 获取列表数据
const getList = async (isLoadMore = false) => {
	// 防止重复请求
	if (data.isLoading) return;
	try {
		data.isLoading = true;
		if (isLoadMore) {
			if (!data.hasMore) return;
			setLoadStatus('loading');
		} else {
			setLoadStatus('before');
			// 初始加载时显示loading至少1秒，避免闪烁
			await delay(500);
		}
		const res = await communityList(data.queryParams);
		const success = await handleResponseData(res, isLoadMore);
		if (!success) {
			// 请求失败的处理
			setLoadStatus(data.list.length === 0 ? 'empty' : 'success');
		}
	} catch (err) {
		await delay(500);
		setLoadStatus(data.list.length === 0 ? 'empty' : 'success');
	} finally {
		data.isLoading = false;
	}
};
const handleSearch = (e) => {
	if (data.queryParams.keyword == '') {
		uni.showToast({ title: '请输入搜索内容', icon: 'none' });
		return;
	}
	data.queryParams.pageNum = 1;
	getList();
};
const changeIMG = (item, type) => {
	communityChangeLike({ id: item.id, type: type }).then((res) => {
		if (res.code == 200) {
			if (type == 1) {
				toast(item.selfLike ? '取消点赞' : '已点赞');
				item.selfLike = !item.selfLike;
			} else {
				toast(item.selfFlag ? '取消收藏' : '已收藏');
				item.selfFlag = !item.selfFlag;
			}
		}
	});
};
// 清空输入框的方法
const clearInput = () => {
	data.queryParams.keyword = '';
	data.isshow = false;
	data.queryParams.pageNum = 1;
	getList();
};
onShareAppMessage((e) => {
	console.log(e);
	if (e.from === 'button') {
		let obj = e.target.dataset.obj;
		return {
			title: obj.title,
			imageUrl: obj.images[0] ? obj.images[0] : '',
			path: `pages/community/info?id=${obj.id}`
		};
	}
});
onShareTimeline((e) => {
	console.log(e);
	if (e.from === 'button') {
		let obj = e.target.dataset.obj;
		return {
			title: obj.title,
			imageUrl: obj.images[0] ? obj.images[0] : '',
			path: `pages/community/info?id=${obj.id}`
		};
	}
});
</script>
<style lang="scss">
page {
	background: #f7f8f6;
}
.kong {
	width: 360rpx;
	margin: 260rpx auto;
	image {
		width: 360rpx;
		height: 236rpx;
	}
	.text {
		font-family: PingFang SC, PingFang SC;
		font-weight: 500;
		font-size: 24rpx;
		color: #979797;
		margin-top: 50rpx;
		text-align: center;
	}
}
.input_class {
	font-family: PingFang SC, PingFang SC;
	font-weight: 500;
	font-size: 28rpx;
	color: #cacaca;
}
.inputs {
	width: 710rpx;
	height: 80rpx;
	background: #ffffff;
	border-radius: 40rpx;
	margin: 20rpx auto;
	padding: 24rpx 32rpx;
	box-sizing: border-box;
	image {
		width: 32rpx;
		height: 32rpx;
	}
	input {
		flex: 1;
		font-family: PingFang SC, PingFang SC;
		font-weight: 500;
		font-size: 28rpx;
		color: #000;
		padding-left: 15rpx;
	}
}

.content {
	width: 100%;
	margin: -10rpx auto 20rpx;
	position: relative;
	padding: 0rpx 20rpx;
	box-sizing: border-box;
	.take_item {
		width: 100%;
		background: #fff;
		border-radius: 20rpx;
		padding: 20rpx;
		box-sizing: border-box;
		margin: 20rpx auto;
		.take_user {
			.u_l {
				image {
					width: 60rpx;
					height: 60rpx;
					border-radius: 30rpx;
				}
				.flex_r {
					margin-left: 14rpx;
					.t1 {
						font-family: PingFang SC, PingFang SC;
						font-weight: 500;
						font-size: 24rpx;
						color: #000000;
					}
					.t2 {
						font-family: PingFang SC, PingFang SC;
						font-weight: 500;
						font-size: 20rpx;
						color: #8f8f8b;
					}
				}
			}
			.u_r {
				width: 124rpx;
				height: 44rpx;
				background: #ffffff;
				border-radius: 22rpx;
				border: 1px solid #b19561;
				image {
					width: 24rpx;
					height: 24rpx;
					margin-right: 4rpx;
				}
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				font-size: 24rpx;
				color: #b19561;
			}
		}
		.texts {
			.title {
				font-family: PingFang SC, PingFang SC;
				font-weight: bold;
				font-size: 30rpx;
				color: #1b1b1b;
				margin-top: 20rpx;
			}
			.t_content {
				margin-top: 20rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				font-size: 28rpx;
				color: #1b1b1b;
				overflow: hidden;
				text-overflow: ellipsis;
				display: -webkit-box;
				-webkit-line-clamp: 4; //几行就填几
				-webkit-box-orient: vertical;
				white-space: pre-wrap;
			}
		}

		.imglist {
			margin: 20rpx auto;
			display: flex;
			.img_item {
				width: 156rpx;
				height: 156rpx;
				margin-right: 14rpx;
				position: relative;
				image {
					width: 156rpx;
					height: 156rpx;
					border-radius: 12rpx;
				}
			}
			.item_ads {
				width: 156rpx;
				height: 156rpx;
				position: absolute;
				top: 0%;
				font-family: PingFang SC, PingFang SC;
				font-weight: bold;
				font-size: 28rpx;
				color: #ffffff;
				background: rgba(0, 0, 0, 0.5);
				border-radius: 12rpx;
			}
		}
		.comment {
			width: 670rpx;
			margin-top: 20rpx;
			background: #f7f7f7;
			border-radius: 20rpx 20rpx 20rpx 20rpx;
			padding: 18rpx 20rpx;
			box-sizing: border-box;
			.ts {
				white-space: nowrap;
				overflow: hidden;
				text-overflow: ellipsis;
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				font-size: 24rpx;
				color: #1b1b1b;
				text {
					color: #979797;
				}
			}
			.c_imglist {
				margin-top: 20rpx;
				display: flex;
				.img_item {
					width: 148rpx;
					height: 148rpx;
					margin-right: 12rpx;
					position: relative;
					image {
						width: 148rpx;
						height: 148rpx;
						border-radius: 12rpx;
					}
				}
				.item_ads {
					width: 148rpx;
					height: 148rpx;
					position: absolute;
					top: 0%;
					font-family: PingFang SC, PingFang SC;
					font-weight: bold;
					font-size: 28rpx;
					color: #ffffff;
					background: rgba(0, 0, 0, 0.5);
					border-radius: 12rpx;
				}
			}
		}

		.hd {
			margin-top: 20rpx;
			height: 52rpx;
			background: #eee8d8;
			border-radius: 80rpx 80rpx 80rpx 80rpx;
			padding: 0rpx 24rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: 500;
			font-size: 28rpx;
			color: #75613e;
			display: inline-block;
			line-height: 52rpx;
			image {
				width: 28rpx;
				height: 28rpx;
				margin-right: 10rpx;
				vertical-align: middle;
			}
		}

		.handle_block {
			margin-top: 20rpx;
			image {
				width: 48rpx;
				height: 48rpx;
			}
			.handle_l {
				.input {
					margin-left: 16rpx;
					width: 284rpx;
					height: 48rpx;
					background: #f7f7f7;
					border-radius: 24rpx;
					font-family: PingFang SC, PingFang SC;
					font-weight: 500;
					font-size: 24rpx;
					color: #d1d1d6;
					line-height: 48rpx;
					text-align: center;
				}
			}
			.handle_r {
				position: relative;
				image {
					margin-left: 50rpx;
				}
				.zf_btns {
					position: absolute;
					width: 50rpx;
					height: 50rpx;
					right: 0%;
					top: 0%;
					opacity: 0;
				}
			}
		}
	}
}
</style>